import {Node, Prefab, Vec3} from 'cc';
import {CombinationHandler} from "db://assets/Scripts/MVC/Bullet/CombinationHandler";
import {BulletController} from "db://assets/Scripts/MVC/Bullet/BulletController";
import {PoolManager} from "db://assets/Scripts/framework/PoolManager";

/**
 * S型子弹策略
 */
export class SPolicy implements CombinationHandler {

    /**
     * S型子弹策略
     *
     * @param bulletPrefab 子弹模型
     * @param parent 父节点
     * @param playerNode 玩家节点
     * @param speed 移动速度
     */
    combination(bulletPrefab: Prefab, parent: Node, playerNode: Node, speed: Vec3): Node[] {
        // 创建子弹节点列表
        const bulletNodes: Node[] = [];

        // 创建3个子弹
        for (let i = 0; i < 3; i++) {
            // 实例化子弹节点
            const bulletNode = PoolManager.getInstance().getNode(bulletPrefab,parent);
            // 添加至子弹列表
            bulletNodes.push(bulletNode);
        }

        // 子弹左、中、右
        const leftBulletNode = bulletNodes[0];
        const midBulletNode = bulletNodes[1];
        const rightBulletNode = bulletNodes[2];
        const leftBulletController: BulletController = leftBulletNode.getComponent(BulletController);
        const midBulletController: BulletController = midBulletNode.getComponent(BulletController);
        const rightBulletController: BulletController = rightBulletNode.getComponent(BulletController);

        // 设置位置
        const position = playerNode.position;
        leftBulletNode.setPosition(position.x - 0.5, position.y, position.z - 1);
        midBulletNode.setPosition(position.x, position.y, position.z - 1);
        rightBulletNode.setPosition(position.x + 0.5, position.y, position.z - 1);

        // 设置方向
        leftBulletController.uiState.direction.x = -1;
        midBulletController.uiState.direction.x = 0;
        rightBulletController.uiState.direction.x = 1;

        // 设置移动速度
        leftBulletController.uiState.speed = new Vec3(speed.x / 10, speed.y, speed.z);
        midBulletController.uiState.speed = speed;
        rightBulletController.uiState.speed = new Vec3(speed.x / 10, speed.y, speed.z);

        // 返回子弹节点列表
        return bulletNodes;
    }
}